package com.example.cpms.service.impl;

import com.example.cpms.entity.Building;
import com.example.cpms.mapper.BuildingMapper;
import com.example.cpms.service.BuildingService;
import com.example.cpms.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

//    /**
//     *
//     * @return
//     */
//    @Override
//    public List<Building> list() {
//        return buildingMapper.list();
//    }

    /**
     * 删除楼宇
     * @param id
     */
    @Override
    public void delete(Integer id) {
        buildingMapper.deleteById(id);
    }


    /**
     * 新增楼宇
     * @param building
     */
    @Override
    public void add(Building building) {
        building.setStatus(0);//把数据库的字段填充
        buildingMapper.insert(building);
    }

    /**
     * 编辑楼宇
     * @param building
     */
    @Override
    public void update(Building building) {
        buildingMapper.update(building);
    }
//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Long count = buildingMapper.count();
//
//        Integer start = (page - 1) * pageSize;
//        List<Building> buildingList = buildingMapper.page(start,pageSize);
//
//        PageBean pageBean = new PageBean(count,buildingList);
//        return pageBean;
//    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize,String name) {
        PageHelper.startPage(page,pageSize);//使用PageHelper分页查询插件
        List<Building> buildingList = buildingMapper.list(name); //从数据库获取列表数据
        Page<Building> p = (Page<Building>) buildingList; //强转
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }
}
